<?php

namespace app\api\controller;


use app\admin\model\Third;
use app\common\controller\Api;
use EasyWeChat\Factory;
use extendwechat\Message;
use think\Config;

/**
 * 微信服务器
 * @ApiInternal
 */
class Wechat extends Api
{
    protected $noNeedRight = ['*'];
    protected $noNeedLogin = ['*'];

    public function wechatMessage(){
        $config = get_addon_config('third');

//        dump($config['wechatWeb']);exit;

        $app = Factory::officialAccount($config['wechatWeb']);


//
        $app->server->push(function ($message)use ($app) {
            car_log("/weixinMessage/", "weixinMessage.log", "收到消息11");
            car_log("/weixinMessage/", "weixinMessage.log", $message);
            switch ($message['MsgType']) {
                case 'event':
                    if($message["Event"] == "subscribe"){
                        $user = $app->user->get($message["FromUserName"]);
                        $user['nickname'] = "微信用户";
                        $loginret = \addons\third\library\Service::connect("wechatWeb", $user, []);
                        car_log("/weixinMessage/", "weixinMessage.log", $loginret);

                    }
                    if($message['Event'] == "unsubscribe"){
                        Third::where("openid",$message["FromUserName"])->delete();
                    }

                case 'text':
//                    return '收到文字消息';
                    break;
                case 'image':
//                    return '收到图片消息';
                    break;
                case 'voice':
//                    return '收到语音消息';
                    break;
                case 'video':
//                    return '收到视频消息';
                    break;
                case 'location':
//                    return '收到坐标消息';
                    break;
                case 'link':
//                    return '收到链接消息';
                    break;
                case 'file':
//                    return '收到文件消息';
                    // ... 其它消息
                default:
//                    return '收到其它消息';
                    break;
            }

            // ...
        });
        $response = $app->server->serve();
        $response->send();
    }




}
